package tree.迭代bfs;

import po.TreeNode;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 在每个树行中找最大值515.java
 * @Description https://leetcode-cn.com/problems/find-largest-value-in-each-tree-row/
 * @createTime 2021年06月01日 19:56:00
 */
public class 在每个树行中找最大值515 {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        if (root == null) {
            return list;
        }
        Deque<TreeNode> deque=new LinkedList<>();
        deque.offer(root);
        while (!deque.isEmpty()) {
            Integer temp=Integer.MIN_VALUE;
            int size = deque.size();
            for (int i = 0; i <size ; i++) {
                TreeNode node = deque.poll();
                temp=Math.max(temp,node.val);
                if (node.left != null) {
                    deque.offer(node.left);
                }
                if (node.right != null) {
                    deque.offer(node.right);
                }
            }
            list.add(temp);
        }
        return list;
    }
}
